﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Ausy.Survey.Common.Providers;
using Ausy.Servey.AccountProvider.Contract;

namespace Ausy.Servey.AccountProvider.DataProvider
{
    /// <summary>
    /// Cette classe représente un gestionnaire d'accès aux données pour les comptes utilisateur
    /// </summary>
    public class AccountDataProvider : BaseDataProvider
    {

        /// <summary>
        /// Cette méthode permet de créer un compte utilisateur en retournant vrai si le compte est crée
        /// </summary>
        /// <param name="account">Compte utilisateur</param>
        /// <returns></returns>
        public bool CreateAccount(Account account)
        {
            var result = false;
            using (var connection = this.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                var command = this.CreateCommand(connection, "CreateUserAccount");
                command.Transaction = transaction;
                command.Parameters.Add(this.CreateParameter("@UserAccountID", DbType.Guid, account.Identifier));
                command.Parameters.Add(this.CreateParameter("@UserName", DbType.String, account.LastName));
                command.Parameters.Add(this.CreateParameter("@UserFirstName", DbType.String, account.FirstName));
                command.Parameters.Add(this.CreateParameter("@UserEmail", DbType.String, account.Email));
                command.Parameters.Add(this.CreateParameter("@UserPhone", DbType.String, account.Phone));
                command.Parameters.Add(this.CreateParameter("@UserFunction", DbType.String, string.IsNullOrEmpty(account.Function) ? DBNull.Value : (object)account.Function));
                command.Parameters.Add(this.CreateParameter("@UserCompanyName", DbType.String, string.IsNullOrEmpty(account.CompanyName) ? DBNull.Value : (object)account.CompanyName));
                command.Parameters.Add(this.CreateParameter("@UserExperience", DbType.String, string.IsNullOrEmpty(account.Experience) ? DBNull.Value : (object)account.Experience));
               
                var returnValueCommand = command.ExecuteScalar();
                result = Convert.ToInt32(returnValueCommand).Equals(1);
                
                transaction.Commit();
                connection.Close();
            }

            return result;
        }
    }
}
